package com.aaa.service.impl;

import com.aaa.dao.DeptDao;
import com.aaa.entity.Dept;
import com.aaa.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


/**
 * @author C-Q
 * @version 1.1.0
 * @Description: TODO
 * @date 2022/12/7
 */
/*          缓存：非业务代码
     核心业务代码: Dept dept = deptDao.selectById(id);*/

@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptDao deptDao;


    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Override
    //cacheNames:缓存的名称
    //key:唯一区分，#id读取方法参数名的值
    @Cacheable(cacheNames="redis::dept",key = "#id")
    /*根据id查询*/
    public Dept findById(Integer id) {
        //核心业务代码
        Dept dept = deptDao.selectById(id);
        return dept;
    }
    @CacheEvict(cacheNames="redis::dept",key = "#id")
    @Override
    public Integer delete(Integer id) {
        int i = deptDao.deleteById(id);
        return i;
    }

    @Override
    public Dept save(Dept dept) {
        deptDao.insert(dept);
        return dept;
    }
    @CachePut(cacheNames="redis::dept",key = "#dept.deptno")
    @Override
    public Dept update(Dept dept) {
        deptDao.updateById(dept);
        return dept;
    }
}
